package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.IliquormallLibraryMapper;
import org.linlinjava.litemall.db.domain.IliquormallLibrary;
import org.linlinjava.litemall.db.domain.IliquormallLibraryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ILiquormallLibraryService {

    @Resource
    IliquormallLibraryMapper liquorLibraryMapper;

    public int insertLiquorLibrary(IliquormallLibrary liquorLibrary) {
        return liquorLibraryMapper.insert(liquorLibrary);
    }

    public List<IliquormallLibrary> queryLiquorLibraryList(String name, String province, String type, String brand, Integer page, Integer limit) {
        IliquormallLibraryExample example = new IliquormallLibraryExample();
        IliquormallLibraryExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(province)) {
            criteria.andProvinceLike("%" + province + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            criteria.andTypeLike("%" + type + "%");
        }
        if (!StringUtils.isEmpty(brand)) {
            criteria.andBrandLike("%" + brand + "%");
        }

        example.setOrderByClause("id asc");

        PageHelper.startPage(page, limit);

        return liquorLibraryMapper.selectByExampleWithBLOBs(example);

    }

    public int updateLiquorLibrary(IliquormallLibrary liquorLibrary) {
        return liquorLibraryMapper.updateByPrimaryKeyWithBLOBs(liquorLibrary);
    }

    public int deleteLiquorLibrary(Integer id) {
        return liquorLibraryMapper.deleteByPrimaryKey(id);
    }

    public IliquormallLibrary selectById(Integer id) {
        return liquorLibraryMapper.selectByPrimaryKey(id);
    }

    public boolean batchInsertLiquorLibrary(List<IliquormallLibrary> libraryList) {
        return liquorLibraryMapper.batchInsert(libraryList);
    }

    public List<IliquormallLibrary> queryDistinctListByCondition(String province, String type, String brand) {
        IliquormallLibraryExample example = new IliquormallLibraryExample();
        IliquormallLibraryExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(province)) {
            criteria.andProvinceEqualTo(province);
        }
        if (!StringUtils.isEmpty(type)) {
            criteria.andTypeEqualTo(type);
        }
        if (!StringUtils.isEmpty(brand)) {
            criteria.andBrandEqualTo(brand);
        }

        example.setDistinct(true);

        return liquorLibraryMapper.selectByExampleSelective(example,
                IliquormallLibrary.Column.province,
                IliquormallLibrary.Column.type,
                IliquormallLibrary.Column.brand);

    }

}
